package cn.yougou.controller;

import cn.yougou.common.DateUtil8;
import cn.yougou.common.DtoUtil;
import cn.yougou.dto.Dto;
import cn.yougou.pojo.YgCart;
import cn.yougou.service.shopcart.YgCartService;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.*;

/**
 * (YgCart)表控制层
 *
 * @author 白杰崇
 * @since 2021-01-07 16:34:41
 */
@RestController
@RequestMapping("ygCart")
public class YgCartController {

    /**
     * 服务对象
     */
    @Reference
    private YgCartService ygCartService;

    /**
     * 添加购物车
     */
    @PostMapping("/create")
    public Dto create(@RequestBody YgCart ygCart) {
        if(null != ygCart){
            return ygCartService.create(ygCart);
        }
        return DtoUtil.returnFail("缺少必要参数", "50001");
    }

    /**
     * 查询该用户购物车
     *
     * @return
     */
    @GetMapping("/findAll")
    public Dto findAll(Integer userId) {
        if (null != userId) {
            YgCart ygCart = new YgCart();
            ygCart.setUserid(userId);
            return ygCartService.queryAll(ygCart);
        }
        return DtoUtil.returnFail("缺少必要参数", "50001");
    }

    /**
     * 清空购物车
     * @return
     */
    @GetMapping("/emptyCart")
    public Dto emptyCart(Integer userId) {
        if(null != userId){
           int config = ygCartService.emptyCart(userId);
           if(config > 0){
               return DtoUtil.returnSuccess();
           }
        }
        return DtoUtil.returnFail("缺少必要参数", "50001");
    }

    /**
     * 删除购物车商品
     * @return
     */
    @PostMapping("/deleteById")
    public Dto deleteById(@RequestBody YgCart ygCart){
        if(null != ygCart.getId()){
            return ygCartService.deleteById(ygCart);
        }
        return DtoUtil.returnFail("缺少必要参数", "50001");
    }

    /**
     * 修改购物车单个商品的 选中状态，数量
     */
    @PostMapping("/amendCart")
    public Dto amendCart(@RequestBody YgCart ygCart){
        if(null != ygCart.getId()){
            return ygCartService.amendCart(ygCart);
        }
        return DtoUtil.returnFail("缺少必要参数", "50001");
    }

    /**
     * 全选 反选
     */
    @GetMapping("/selectAll")
    public Dto selectAll(Integer userId,Boolean selectAll){
        if(null != userId && null != selectAll){
            return ygCartService.selectAll(userId,selectAll);
        }
        return DtoUtil.returnFail("缺少必要参数", "50001");
    }

    /**
     * 获取用户购物车全部选中的商品
     * @param userId
     * @return
     */
    @GetMapping("/submitOrder")
    public Dto submitOrder(Integer userId){
        if(null != userId){
            return ygCartService.submitOrder(userId);
        }
        return DtoUtil.returnFail("缺少必要参数", "50001");
    }



}